package com.uplift.mobile.realty.controller;

import com.uplift.common.base.ServiceReslut;
import com.uplift.common.result.IResultable;
import com.uplift.common.result.ResultWrapper;
import com.uplift.common.result.ResultWrapper;
import com.uplift.mobile.realty.model.RealtyFileModel;
import com.uplift.mobile.realty.service.IRealtyFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 文件相关
 * 
 * @author bcwang
 *
 */
@Controller
@RequestMapping(value = "/realty-file")
public class RealtyFileController {

	@Autowired
	private IRealtyFileService realtyFileService;


	/**
	 * 查询物业附件信息
	 *
	 * @param request
	 * @param realtyFileModel
	 * @return IResultable
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public IResultable list(HttpServletRequest request, RealtyFileModel realtyFileModel) {
		try {
			List<Map<String, Object>> list = this.realtyFileService.queryRealtyFile(realtyFileModel);
			return ResultWrapper.wrapListPage(list, ResultWrapper.SUCC, "查询成功");
		} catch (Exception e) {
			return ResultWrapper.wrapListPage(null, ResultWrapper.FAIL, "查询失败");
		}
	}

	/**
	 * 新增文件记录
	 *
	 * @param request
	 * @param
	 * @return IResultable
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public IResultable save(HttpServletRequest request, RealtyFileModel realtyFileModel) {
		try {
			ServiceReslut serviceReslut = this.realtyFileService.addRealtyFile(realtyFileModel);
			if (serviceReslut.getCode() > 0) {
				return ResultWrapper.wrapMap(null, ResultWrapper.SUCC, "保存成功");
			} else {
				return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "保存失败," + serviceReslut.getMessage());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "保存失败");
		}
	}

	/**
	 * 更新文件记录
	 *
	 * @param request
	 * @param
	 * @return IResultable
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public IResultable update(HttpServletRequest request, RealtyFileModel realtyFileModel) {
		try {
			ServiceReslut serviceReslut = this.realtyFileService.updateRealtyFile(realtyFileModel);
			if (serviceReslut.getCode() > 0) {
				return ResultWrapper.wrapMap(null, ResultWrapper.SUCC, "更新成功");
			} else {
				return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "更新失败," + serviceReslut.getMessage());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "更新失败");
		}
	}

	/**
	 * 删除文件记录
	 *
	 * @param request
	 * @param realtyFileModel
	 * @return IResultable
	 */
	@RequestMapping(value = "/remove")
	@ResponseBody
	public IResultable remove(HttpServletRequest request, RealtyFileModel realtyFileModel) {
		try {
			ServiceReslut serviceReslut = this.realtyFileService.deleteRealtyFile(realtyFileModel);
			if (serviceReslut.getCode() > 0) {
				return ResultWrapper.wrapMap(null, ResultWrapper.SUCC, "删除成功");
			} else {
				return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "删除失败," + serviceReslut.getMessage());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "删除失败");
		}
	}
}
